package gang.com.mobelhelper.ui.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import gang.com.mobelhelper.MainActivity;
import gang.com.mobelhelper.R;
import gang.com.mobelhelper.common.constant.Constant;
import gang.com.mobelhelper.common.util.ACache;
import gang.com.mobelhelper.ui.adapter.GuideFragmentAdapter;
import gang.com.mobelhelper.ui.fragment.GuideFragment;
import gang.com.mobelhelper.ui.widget.CircleIndicator;

/**
 * 点点点下面的滑动我们可以使用继承自ViewPager.OnPageChangeListener
 *
 * CircleIndicator类使用
 */
public class GuiderActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.btn_enter)
    Button btnEnter;
    @BindView(R.id.activity_guide)
    RelativeLayout activityGuide;
    @BindView(R.id.indicator)
    CircleIndicator indicator;
    private Unbinder mUnbinder;
    GuideFragmentAdapter mGuideFragmentAdapter;
    List<Fragment> fragments = new ArrayList<>();
    public static final String IMG_ID = "IMG_ID";
    public static final String TEXT_ID = "TEXT_ID";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guider);
        mUnbinder = ButterKnife.bind(this);
        initData();
    }

    private void initData() {

        fragments.add(GuideFragment.newInstance(R.drawable.guide_1, R.color.color_bg_guide1, R.string.guide_1));
        fragments.add(GuideFragment.newInstance(R.drawable.guide_2, R.color.color_bg_guide2, R.string.guide_2));
        fragments.add(GuideFragment.newInstance(R.drawable.guide_3, R.color.color_bg_guide3, R.string.guide_3));
        //对于这种有些人就会有一个fragment就创建几个Fragment的笨方法，我们发现一般开发过程中只会引导页的图片不一样
        //我们可以用一个Fragment构造出来
        mGuideFragmentAdapter = new GuideFragmentAdapter(getSupportFragmentManager());
        mGuideFragmentAdapter.setmFragments(fragments);
        viewpager.setCurrentItem(0);
        viewpager.setOffscreenPageLimit(mGuideFragmentAdapter.getCount());
        viewpager.setAdapter(mGuideFragmentAdapter);
        viewpager.addOnPageChangeListener(this);
        //viewpage滑动动画添加
        // viewpager.setPageTransformer();
        indicator.setViewPager(viewpager);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
      /*  if (position == 0) {
         //设置第一个为红色，其他的两个为白色，下面的两个同理
        } else if (position == 1) {

        }*/
        //如果是最后一页显示，不是最后一页隐藏
        btnEnter.setVisibility((position == mGuideFragmentAdapter.getCount() - 1) ? View.VISIBLE : View.GONE);
       Bundle a= fragments.get(position).getArguments();
        int hehe = a.getInt(TEXT_ID);
        Log.e("卧槽困啊",hehe+"啦啦啦");
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick(R.id.btn_enter)
    public void onViewClicked() {
        ACache.get(this).put(Constant.IS_SHOW_GUIDE, 0);
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}
